package com.cola.utils.templateMessageUtils.utils;

import com.cola.utils.DBControl;
import com.cola.utils.DateUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;

/**
 * Created by Max on 2017/8/18.
 * 模板消息公共类
 */
public class TemplateCommon {
    private static final Logger logger = LoggerFactory.getLogger(TemplateCommon.class);
    private static QueryRunner queryRunner = null;
    private static QueryRunner getQueryRunner(){
        if (null == queryRunner) {
            queryRunner = new QueryRunner();
        }
        return queryRunner;
    }

    /**
     * 插入待发送模板下消息
     * @param orderNo
     * @param messageType
     * @param openId
     * @param messageJson
     * @return
     */
    public static boolean insetWaitingSendMessage(String orderNo,String messageType,String openId,String messageJson){
        Connection conn = null;
        try{
            conn = DBControl.getConnection();
            String sql = "insert into cola_message_waiting_send (order_no,message_type,receiver_open_id,message_content,send_status,create_time)" +
                    " values(?,?,?,?,0,?)";
            return getQueryRunner().update(conn,sql,orderNo,messageType,openId,messageJson, DateUtils.getCurrentDateTime())>0;

        }catch (Exception e){
            logger.error("插入待发送模板消息异常!",e);
        }finally {
            DbUtils.closeQuietly(conn);
        }
        return false;
    }
}
